// \$Id: account.js 313 2011-11-18 04:09:24Z bobcat $

J(document).ready(function() {
  ultraCart.init(cartConfig);
});

// login and redirect customer
function login(email, password, create, referrer) {
  var JStatusBox, customer, status, ref, err;
  if (create) {
    JStatusBox = J('form#create input.button').parent().next();
  } else {
    JStatusBox = J('form#login input.button').parent().next();
  }
  JStatusBox.html('');
  customer = ultraCart.loginCustomerProfile(email, password);
  if (customer) {
    // success, redirect
    status = 'Login successful, redirecting...';
    JStatusBox.removeClass('error');
    JStatusBox.addClass('success');
    JStatusBox.html(status);
    ref = referrer.replace('reviewSave', 'reviewLoad');
    window.location = ref;
  } else {
    // errors, stay
    err = 'Please verify email and/or password.';
    JStatusBox.removeClass('success');
    JStatusBox.addClass('error');
    JStatusBox.html(err);
  }
  return false;
}

// reset password, dispatch email
function resetPassword(email) {
  var status;
  ultraCart.resetCustomerProfilePassword(email);
  status = 'A new password has been sent to ' + email + '.';
  J('form#pwreset input.button').attr('disabled', 'disabled');
  J('form#pwreset input.button').parent().next().addClass('success');
  J('form#pwreset input.button').parent().next().html(status);
  return false;
}

// create account then redirect
function createAccount() {
  var email = J('#new_email').val();
  var password = J('#new_password').val();
  ultraCart.establishCustomerProfileImmediately(email, password);
  login(email, password, true);
}
